package cst.a931xmc.yuejian20.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import cst.a931xmc.yuejian20.Bean.RegisterBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.RegisterModel;
import cst.a931xmc.yuejian20.R;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText user_tel;
    private EditText user_password;
    private EditText user_password2;
    private ImageButton btn_back;
    private Button btn_reg;
    private RegisterModel model;
    private Listener<RegisterBean> listener;

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
        initData();
    }

    public void init(){
        user_tel = findViewById(R.id.user_tel);
        user_password = findViewById(R.id.user_password);
        user_password2 = findViewById(R.id.user_password2);
        btn_back = findViewById(R.id.btn_back);
        btn_reg = findViewById(R.id.btn_reg);
        btn_reg.setOnClickListener(this);
        btn_back.setOnClickListener(this);
    }

    public void initData(){
        listener = new Listener<RegisterBean>() {
            @Override
            public void onResponse(RegisterBean l) {
                if(!l.getSuccess().equals("0")){
                    Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(RegisterActivity.this, "该账号已存在", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(RegisterActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_reg:
                if(TextUtils.isEmpty(user_password.getText().toString())){
                    Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
                }
                else {
                    if (!TextUtils.isEmpty(user_password.getText().toString()) &&
                            TextUtils.equals(user_password.getText().toString(), user_password2.getText().toString())) {
                        model = new RegisterModel();
                        model.register(user_tel.getText().toString(),
                                user_password.getText().toString(),
                                listener);
                    } else {
                        Toast.makeText(RegisterActivity.this, "两次输入密码不符", Toast.LENGTH_LONG).show();
                    }
                }
                break;
            case R.id.btn_back:
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}
